/* FluxamasynthCrossFading.pde
 * Demonstrates cross fading on the Modern Device Fluxamasynth Shield
 * ModernDevice.com
 */

#include <FlexamySynth.h>
#include <PgmChange.h>

FlexamySynth synth;

byte tone1 = NOTE_E + OCTAVE*4;
byte tone2 = NOTE_B + OCTAVE*4;
byte tone3 = NOTE_E + OCTAVE*5;

byte vol1 = 64;          // Volumes must be inverses of each other
byte vol2 = 127 - vol1;  // or they'll get out of sync

byte i = 1;              // Step size
byte dir = 1;            // Direction to step in: 1 = up, -1 = down

void setup() 
{
  synth.begin();
  synth.GM_Reset();
  
  synth.programChange( 0, GM_SynthStrings2 ); // give our two channels different voices
  synth.programChange( 1, GM_Pad_Polysynth );

  synth.setChannelVolume( 0, vol1 );    // set their volumes
  synth.setChannelVolume( 1, vol2 );

  synth.noteOn( 0, tone1, 127 );        // turn on notes for each channel
  synth.noteOn( 0, tone2, 127 );
  synth.noteOn( 1, tone2, 127 );
  synth.noteOn( 1, tone3, 127 );
}

void loop() 
{
  while (vol1 > i && vol1 < (127 - i))  // loops while in valid volume range
  {
    delay( 20 );
    vol1 += i * dir;                     // vol1 increments by i in direction dir
    vol2 = 127 - vol1;                  // vol2 is always the inverse
    synth.setChannelVolume( 0, vol1 );  // update volumes
    synth.setChannelVolume( 1, vol2 );
  }
  dir *= -1;                            // once volumes reach bounds, reverse dir
  vol1 += i * dir;                      // increment once in new direction
  byte vol2 = 127 - vol1;                // to ensure volumes are back in range
}
